package com.woniuxy.shiro;


import com.woniuxy.shiro.filter.JWTFilter;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName ShiroConfig
 * @Description Shiro过滤器
 * @Author 王维鑫
 * @Date 2021/5/13 0013 10:34
 * @Version 1.0
 */
@Configuration
public class ShiroConfig {

    @Bean
    public JWTRealm initUserRealm(){
        return new JWTRealm();
    }


    /**
     *@Author 王维鑫
     *@Description 初始化安全管理器
     *@Param []
     *@return org.apache.shiro.web.mgt.DefaultWebSecurityManager
     **/
    @Bean
    public DefaultWebSecurityManager initSecurityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(initUserRealm());
        return securityManager;
    }

    /**
     *@Author 王维鑫
     *@Description 配置shiro过滤器
     *@Param []
     *@return org.apache.shiro.spring.web.ShiroFilterFactoryBean
     **/
    @Bean
    public ShiroFilterFactoryBean shiroFilter() throws UnsupportedEncodingException {
        //实例化Shiro过滤器工厂
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //在工厂中注入安全管理器
        shiroFilterFactoryBean.setSecurityManager(initSecurityManager());
        //将我们自己的filter添加到Shiro
        Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
        filters.put("jwt",new JWTFilter());
        //创建一个有序键值对用于存储黑白名单
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        //anon表示无须登陆就能访问的资源地址

        filterChainDefinitionMap.put("/user/login", "anon");
        filterChainDefinitionMap.put("/*.html", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**", "anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        filterChainDefinitionMap.put("/v2/**", "anon");
        filterChainDefinitionMap.put("/swagger-ui.html/**", "anon");
        //登出
        filterChainDefinitionMap.put("/logout","logout");
        //其余所有请求地址均需要通过jwt校验
        filterChainDefinitionMap.put("/**", "jwt");
        //将黑白名单配置到shiro过滤器
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }
    /**
     *@Author 王昊波
     *@Description 权限校验AOP配置
     *@Param []
     *@return org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
        AuthorizationAttributeSourceAdvisor advisor=new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(initSecurityManager());
        return advisor;
    }
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator app=new DefaultAdvisorAutoProxyCreator();
        app.setProxyTargetClass(true);
        return app;
    }
}
